/**
 * @filename:CapitalTypeServiceImpl 2023-4-20
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.MoldAndInspectDao;
import com.wonder.app.mapper.MoldMaterialDao;
import com.wonder.app.mapper.MoldScrapDao;
import com.wonder.app.request.MoldAndInspectRequest;
import com.wonder.app.request.MoldScrapRequest;
import com.wonder.app.service.MoldAllocationService;
import com.wonder.app.service.MoldAndInspectService;
import com.wonder.app.service.MoldScrapService;
import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:TODO(模具链类型服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 *
 */
@Service

public class MoldScrapServiceImpl extends ServiceImpl<MoldScrapDao, MoldScrap> implements MoldScrapService {

    @Autowired
    private MoldScrapService moldScrapService;
    @Autowired
    private MoldScrapDao moldScrapDao;
    @Autowired
    private MoldAllocationService moldAllocationService;

    @Autowired
    private MoldAndInspectService moldAndInspectService;
    @Autowired
    private MoldMaterialDao moldMaterialDao;
    @Override
    public String validate(MoldScrap moldScrap, String type) {
        return "";
    }

    @Override
    public IPage<MoldScrap> selectList(MoldScrapRequest request, Page<MoldScrap> page) {
        LambdaQueryWrapper<MoldScrap> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (null != request.getId()) {
            lambdaQueryWrapper.eq(MoldScrap::getId, request.getId());
        }
        if (request.getBillNo() != null){
            lambdaQueryWrapper.like(MoldScrap::getBillNo,request.getBillNo());
        }

        if (request.getInspectNo() != null){
            lambdaQueryWrapper.like(MoldScrap::getInspectNo,request.getInspectNo());
        }

        if (request.getAllocationNo() != null){
            lambdaQueryWrapper.like(MoldScrap::getAllocationNo,request.getAllocationNo());
        }

        if (request.getMaterialNo() != null){
            lambdaQueryWrapper.like(MoldScrap::getMaterialNo,request.getMaterialNo());
        }
        if (request.getMaterialName() != null){
            lambdaQueryWrapper.like(MoldScrap::getMaterialName,request.getMaterialName());
        }
        if (request.getStockNo() != null){
            lambdaQueryWrapper.like(MoldScrap::getStockNo,request.getStockNo());
        }
        if (request.getStockName() != null){
            lambdaQueryWrapper.like(MoldScrap::getStockName,request.getStockName());
        }
        if (request.getLocationName() != null){
            lambdaQueryWrapper.like(MoldScrap::getLocationName,request.getLocationName());
        }
        if (request.getLocationNo() != null){
            lambdaQueryWrapper.like(MoldScrap::getLocationNo,request.getLocationNo());
        }
        if (request.getCreateUserName() != null){
            lambdaQueryWrapper.like(MoldScrap::getCreateUserName,request.getCreateUserName());
        }

        if (request.getCreateTime() != null){
            Date[] orderDate = request.getCreateTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(MoldScrap::getCreateTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(MoldScrap::getCreateTime,orderDate[1]);

        }

        lambdaQueryWrapper.orderByDesc(MoldScrap::getId);
        Page<MoldScrap> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    /**
     * 生成报废单
     * @param userName
     * @param
     * @return
     */
    @Override
    public boolean createScrp(String userName,  MoldInspect moldInspect){
        String BILLNO =  IXqcConst.createBillNo(BillType.MJBF);//生成模具报废单号
        MoldScrap moldScrap = new MoldScrap();
        moldScrap.setBillNo(BILLNO);
        moldScrap.setCreateTime(new Date());
        moldScrap.setCreateUserName(userName);
        moldScrap.setMaterialNo(moldInspect.getMaterialNo());
        moldScrap.setMaterialName(moldInspect.getMaterialName());
        moldScrap.setInspectNo(moldInspect.getBillNo());//检查单号
        moldScrap.setStockNo(moldInspect.getStockNo());
        moldScrap.setStockName(moldInspect.getStockName());
        moldScrap.setLocationNo(moldInspect.getLocationNo());
        moldScrap.setLocationName(moldInspect.getLocationName());

        //生成报废的调拨单
        MoldAllocation moldAllocation = moldAllocationService.createMoldAllocation(userName, moldScrap);
        //报废单关联调拨单号
        moldScrap.setAllocationNo(moldAllocation.getBillNo());

        boolean b = moldScrapService.saveOrUpdate(moldScrap);//保存报废单
        //添加报废模具链
        MoldMaterial moldMaterial = moldMaterialDao.selectOne(new LambdaQueryWrapper<MoldMaterial>()
                .eq(MoldMaterial::getLocationNo, moldInspect.getLocationNo()));
        Map<String,Object> insMap = new HashMap<>();
        insMap.put("moldId",moldMaterial.getId());
        insMap.put("orderTime",moldScrap.getCreateTime());
        insMap.put("orderNo",moldScrap.getBillNo());
        insMap.put("orderType",3);//报废单
        moldAndInspectService.createAndInspect(userName,insMap);

        //调拨模具链
        Map<String,Object> allocationMap = new HashMap<>();
        allocationMap.put("moldId",moldMaterial.getId());
        allocationMap.put("orderTime",moldAllocation.getCreateTime());
        allocationMap.put("orderNo",moldAllocation.getBillNo());
        allocationMap.put("orderType",4);//报废单
        moldAndInspectService.createAndInspect(userName,allocationMap);
        return b;

    }

    /**
     * 修改报废单同步生成调拨单
     * @param userName
     * @param map
     * @return
     */
    @Override
    public boolean updateScrp(String userName, Map<String, Object> map){
        MoldScrap moldScrap = moldScrapDao.selectById(VtuzxUtil.getInt(map,"id"));
//        String allocationbillno = moldAllocationService.createMoldAllocation(userName,map);
//        if (allocationbillno.equals("")){
//            throw new RuntimeException(String.format("单号%s的报废单未生成调拨单", VtuzxUtil.getString(map,"billNo")));
//        }else {
//            moldScrap.setAllocationNo(allocationbillno);
//            return moldScrapService.saveOrUpdate(moldScrap);
//        }
        return true;
    }

}
